package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.query.JSONResult;
import cn.wolfcode.crm.query.PageResult;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@Controller
@RequestMapping("employee")
public class EmployeeController {

    @Autowired

    private IEmployeeService employeeService;

    //由于dataGrid组件返回必须为json,所以跳转界面就分开
    @RequestMapping("view")
    @RequiresPermissions(value = {"employee:view", "员工列表"}, logical = Logical.OR)
    public String view() {
        return "employee";
    }

    //页面需要的json数据
    @RequestMapping("query")
    @ResponseBody
    public PageResult query(EmployeeQueryObject qo) {
        return employeeService.query(qo);
    }

    //表单用户名的校验
    @RequestMapping("validateByName")
    @ResponseBody
    public Boolean validateByName(String username, Long id) {
        return employeeService.selectByName(username, id);
    }

    //修改员工状态
    @RequestMapping("changeState")
    @ResponseBody
    public JSONResult changeState(Long id) {
        JSONResult result = new JSONResult();
        try {
            employeeService.changeState(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    //修改密码
    @RequestMapping("changePassword")
    @RequiresPermissions(value = {"employee:changePassword", "修改密码"}, logical = Logical.OR)
    @ResponseBody
    public JSONResult changePassword(Long id, String newPassword) {
        JSONResult result = new JSONResult();
        try {
            employeeService.changePassword(id, newPassword);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    //编辑操作
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Employee employee) {
        JSONResult result = new JSONResult();
        try {
            employee.setState(true);
            employeeService.saveOrUPdate(employee);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    //导出
    @RequestMapping("export")
    public void export(EmployeeQueryObject qo,HttpServletResponse response) throws IOException {
        //设置响应头
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
        //创建文件对象
        Workbook wb = new HSSFWorkbook();
        //创建工作簿
        Sheet sheet = wb.createSheet("employeeData");
        //查询员工数据(要带高级查询的参数)
        List<Employee> employeeList = employeeService.selectExportData(qo);
        //创建第一行
        Row row = sheet.createRow(0);
        //设置标题
        row.createCell(0).setCellValue("用户名");
        row.createCell(1).setCellValue("真实姓名");
        row.createCell(2).setCellValue("电话");
        row.createCell(3).setCellValue("密码");
        //循环数据
        for (int i = 0; i < employeeList.size(); i++) {
            //从第二行开始创建行
            Row row1 = sheet.createRow(i + 1);
            Employee employee = employeeList.get(i);
            //创建单元格
            row1.createCell(0).setCellValue(employee.getUsername());
            row1.createCell(1).setCellValue(employee.getRealname());
            row1.createCell(2).setCellValue(employee.getTel());
            row1.createCell(3).setCellValue(employee.getPassword());
        }

        //输出到单元格
        wb.write(response.getOutputStream());
    }

    //导入
    @RequestMapping("importData")
    public String importData(MultipartFile file) throws IOException {
        employeeService.importData(file);
        return "redirect:/employee/view.do";
    }
}
